package com.android.calldelay;

import java.util.ArrayList;

import com.android.calldelay.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.BaseColumns;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class ContactListTaskDialog extends
		AsyncTask<Void, Void, ArrayList<ContactData>> {

	private ContentResolver cr;
	private View viewET;
	
  
	public ContactListTaskDialog(View view) {
		super();
		this.context = view.getContext();
		this.viewET = view;
		this.cr = context.getContentResolver();
 
	}

	private Context context;
	private AlertDialog dialog;
	private ProgressDialog progress;

	@Override
	protected ArrayList<ContactData> doInBackground(Void... params) {

		// TODO Auto-generated method stub
		ArrayList<ContactData> contactDatas = getContactNumbers(cr);
		return contactDatas;
	}

	@Override
	protected void onPostExecute(ArrayList<ContactData> result) {
		// TODO Auto-generated method stub
		super.onPostExecute(result);
		progress.dismiss();
		ContactAdapter adapter = new ContactAdapter(context, result);

		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle("Choose Contact");

		ListView listView = new ListView(context);
		listView.setCacheColorHint(Color.TRANSPARENT);
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

 
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				try {
					ContactData contactData = (ContactData) parent
							.getItemAtPosition(position);
					/*Toast.makeText(
							view.getContext(),
							"Name : " + contactData.getContactName()
									+ "\nNumber : "
									+ contactData.getContactPhoneNumber(),
							Toast.LENGTH_LONG).show();*/
					dialog.dismiss();
				 
					((EditText)viewET).setText(contactData.getContactPhoneNumber());
					
 
					 					
					
					
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();

				}

			}
		});

		builder.setView(listView);
		dialog = builder.create();

		dialog.show();

	}

	@Override
	protected void onPreExecute() {
		// TODO Auto-generated method stub
		super.onPreExecute();
		 progress = ProgressDialog.show(context, 
	                null, "Loading Contacts...");


	}

	class ContactAdapter extends BaseAdapter {

		private ArrayList<ContactData> contactDatas;
		private LayoutInflater inflater;

		public ContactAdapter(Context context,
				ArrayList<ContactData> contactDatas) {
			// TODO Auto-generated constructor stub
			this.contactDatas = contactDatas;
			this.inflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return contactDatas.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return contactDatas.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ViewHandler handler;
			if (convertView == null) {
				handler = new ViewHandler();
				convertView = inflater.inflate(R.layout.contact_itam, null);
				handler.tvName = (TextView) convertView
						.findViewById(R.id.textView_name);
				handler.tvNumber = (TextView) convertView
						.findViewById(R.id.textView_number);

				convertView.setTag(handler);
			} else {

				handler = (ViewHandler) convertView.getTag();
			}

			ContactData contactData = contactDatas.get(position);
			handler.tvName.setText(contactData.getContactName());
			handler.tvNumber.setText(contactData.getContactPhoneNumber());

			return convertView;
		}

		class ViewHandler {
			TextView tvName, tvNumber;
		}

	}

	private ArrayList<ContactData> getContactNumbers(
			ContentResolver contentResolver) {

		ArrayList<ContactData> contactDatas = new ArrayList<ContactData>();
		contactDatas.clear();
		String contactPhoneNumber = null;
		int contactNumberType = Phone.TYPE_MOBILE;
		String contactName = null;

		if (contactDatas.size() <= 0) {
			ContentResolver cr = contentResolver;
			Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
					null, null, null);
			if (cur.getCount() > 0) {
				while (cur.moveToNext()) {
					String id = cur.getString(cur
							.getColumnIndex(BaseColumns._ID));
					contactName = cur
							.getString(cur
									.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
					ContactData contactData = new ContactData();
					contactData.setContactName(contactName);
					if (Integer
							.parseInt(cur.getString(cur
									.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
						Cursor phones = cr
								.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
										null,
										ContactsContract.CommonDataKinds.Phone.CONTACT_ID
												+ " = ?", new String[] { id },
										null);

						while (phones.moveToNext()) {
							contactPhoneNumber = phones.getString(phones
									.getColumnIndex(Phone.NUMBER));
							contactData
									.setContactPhoneNumber(contactPhoneNumber);
							contactDatas.add(contactData);
							contactNumberType = phones.getInt(phones
									.getColumnIndex(Phone.TYPE));
						}
						phones.close();
					}
				}
			}// end of contact name cursor
			cur.close();
		}
		return contactDatas;
	}

}
